package qc.module.sl.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qc.module.sl.dto.station.StationDto;
import qc.module.sl.dto.station.StationQueryConditionDto;
import qc.module.sl.entity.SlBasicStations;
import qc.module.sl.mapper.BasicStationsMapper;
import qc.module.sl.repository.SlBasicStationsRepository;

import java.util.List;

/**
 * BasicStationsService
 *
 * @author QuCheng Tech
 * @create 2023/02/14
 */
@Service
public class BasicStationsService {
    private SlBasicStationsRepository repository;
    
    @Autowired
    public void setSlBasicStationsRepository(SlBasicStationsRepository repository){
        this.repository = repository;
    }
    
    private BasicStationRelationService basicStationRelationService;
    
    @Autowired
    public void setBasicStationRelationService(BasicStationRelationService basicStationRelationService){
        this.basicStationRelationService = basicStationRelationService;
    }

    /**
     * 根据查询条件查询站点信息
     *
     * @param dto 站点信息查询条件dto
     * @return 站点信息集合
     * @author QcCheng Tech
     * @since 2023/2/15
     */
    public List<StationDto> query(StationQueryConditionDto dto) {
        LambdaQueryWrapper<SlBasicStations> wrapper = new LambdaQueryWrapper<>();

        if (dto != null) {
            if (!StringUtils.isBlank(dto.getClassifycode())) {
                List<String> list = basicStationRelationService.queryStcdByClassifyCode(dto.getClassifycode());
                if (list != null)
                    wrapper.in(SlBasicStations::getStcd, list);
            }
            if (!StringUtils.isBlank(dto.getStatus()))
                if (dto.getStatus().equals("0") || dto.getStatus().equals("1"))
                    wrapper.eq(SlBasicStations::getEnable, dto.getStatus());
                
            if (!StringUtils.isBlank(dto.getWords()))
                wrapper.and(k -> k.like(SlBasicStations::getName, dto.getWords()).or().like(SlBasicStations::getDescription, dto.getWords()));
        }
        wrapper.orderByAsc(SlBasicStations::getName);

        List<SlBasicStations> result = repository.selectList(wrapper);

        if (result.size() > 0x0)
            return BasicStationsMapper.MAPPER.toDtoList(result);

        return null;
    }
}
